package com.chenzhou.qx.library;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.bun.miitmdid.core.JLibrary;


/**
 * OAID获取工具
 * @author tk
 * @date 2019/12/16
 */
public class MsaIdHelper implements MiitHelper.AppIdsUpdater {

    private static final String TAG = "MsaIdHelper";
    private static final String SP_SUPPORT = "sp_msaid_is_support";
    private static final String SP_OAID = "sp_msaid_oaid";
    private static final String SP_VAID = "sp_msaid_vaid";
    private static final String SP_AAID = "sp_msaid_aaid";

    private static MsaIdHelper instance;
    private final Context context;
    private boolean debug;
    private String oaid;
    private String vaid;
    private String aaid;

    /**
     * 0 未初始化
     * 1 支持
     * 2 不支持
     */
    private int support;

    private MsaIdHelper(Context context) {
        this.context = context.getApplicationContext();
    }

    public static MsaIdHelper getInstance(Context context) {
        if(instance == null) {
            synchronized (MsaIdHelper.class) {
                if(instance == null) {
                    instance = new MsaIdHelper(context);
                }
            }
        }
        return instance;
    }

    public void init() {
        try {
            JLibrary.InitEntry(context);
            MiitHelper miitHelper = new MiitHelper(this);
            miitHelper.getDeviceIds(context);
        } catch (Exception e) {
            e.printStackTrace();
            //如果初始报错,则不支持oaid
            this.support = 2;
            PreferencesUtils.putInt(context, SP_SUPPORT, support);
        }
    }

    @Override
    public void onIdsAvalid(boolean isSupport, String oaid, String vaid, String aaid) {
        String idstext = "Oaid获取成功："
                + "\n" + "设备是否支持: " + (isSupport ? "true" : "false")
                + "\n" + "OAID: " + oaid
                + "\n" + "VAID: " + vaid
                + "\n" + "AAID: " + aaid;
        if (debug) {
            Log.d(TAG, idstext);
        }
        this.support = isSupport ? 1 : 2;
        this.oaid = oaid;
        this.vaid = vaid;
        this.aaid = aaid;
        if (context != null) {
            PreferencesUtils.putInt(context, SP_SUPPORT, support);
            PreferencesUtils.putString(context, SP_OAID, oaid);
            PreferencesUtils.putString(context, SP_VAID, vaid);
            PreferencesUtils.putString(context, SP_AAID, aaid);
        }
    }

    public String getOaid() {
        if (context != null && TextUtils.isEmpty(oaid)) {
            oaid = PreferencesUtils.getString(context, SP_OAID, "");
        }
        return oaid;
    }

    public String getVaid() {
        if (context != null && TextUtils.isEmpty(vaid)) {
            vaid = PreferencesUtils.getString(context, SP_VAID, "");
        }
        return vaid;
    }

    public String getAaid() {
        if (context != null && TextUtils.isEmpty(aaid)) {
            aaid = PreferencesUtils.getString(context, SP_AAID, "");
        }
        return aaid;
    }

    public boolean isSupportOaid() {
        if (context != null && support == 0) {
            support = PreferencesUtils.getInt(context, SP_SUPPORT, 0);
        }
        return support == 1;
    }

    public MsaIdHelper setDebug(boolean debug) {
        this.debug = debug;
        return this;
    }
}
